/*******************************************************************

    Author: Xiao
    Date: 2022/3/28 14:13

*******************************************************************/
package utils

import (
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"math/rand"
	"strings"
	"time"
)



/**
 * @Description 生成数字验证码
 * @Param 长度
 * @return code
 **/
func GetVerifyDigitalCode(width int) string {
	numeric := [10]byte{0,1,2,3,4,5,6,7,8,9}
	r := len(numeric)
	rand.Seed(time.Now().UnixNano())

	var sb strings.Builder
	for i := 0; i < width; i++ {
		fmt.Fprintf(&sb, "%d", numeric[ rand.Intn(r) ])
	}
	return sb.String()
}

/**
 * @Description 生成大写英文验证码
 * @Param 长度
 * @return code
 **/
func GetVerifyUpperEnglishCode(width int) string {
	str := "ABCDEFGHJKLMNPQRSTUVWXYZ"
	r := len(str)
	rand.Seed(time.Now().UnixNano())
	var result string
	for i := 0; i < width; i++ {
		result += fmt.Sprintf("%s",string(str[rand.Intn(r)]))
	}
	return result
}

/**
 * @Description 生成全英文验证码
 * @Param 长度
 * @return code
 **/
func GetVerifyEnglishCode(width int) string {
	str := "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	r := len(str)
	rand.Seed(time.Now().UnixNano())

	var result string
	for i := 0; i < width; i++ {
		result += fmt.Sprintf("%s",string(str[rand.Intn(r)]))
	}
	return result
}

/**
 * @Description 生成英文加数字验证码
 * @Param 长度
 * @return code
 **/
func GetVerifyAllCode(width int) string {
	str := "0123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	r := len(str)
	rand.Seed(time.Now().UnixNano())

	var result string
	for i := 0; i < width; i++ {
		result += fmt.Sprintf("%s",string(str[rand.Intn(r)]))
	}
	return result
}

// Description: 根据原始密码进行md5加盐加密
// Param: 原始密码，盐
// return: 加密后的值
func EncodeBySalt(old,salt string) string{
	m := md5.New()
	m.Write([]byte(old))
	m.Write([]byte(salt))
	res := m.Sum(nil)
	return hex.EncodeToString(res)
}
